package com.ocean.admin.extension.security;

import com.ocean.admin.extension.security.core.SysUserDetails;
import com.ocean.admin.extension.security.filter.WebFilterExtend;
import com.ocean.core.util.StringUtil;
import com.ocean.security.PermissionService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 自定义权限验证
 * <p>
 * PreAuthorize("hasAuthority('xx:xx:xx')")
 *
 * @author zhang
 * @since 2022-8-20
 */
@Service("sse")
public class PermissionServiceExtend extends PermissionService {

    /**
     * 是否是管理员或全部权限
     *
     * @return true是
     */
    @Override
    public boolean hasAdmin() {
        SysUserDetails current = WebFilterExtend.current();
        Set<String> setPermission = AuthorityUtils.authorityListToSet(current.getAuthorities());
        return hasPermissions(setPermission, ALL_PERMISSION) || current.getRoles().contains(adminProperties.getKey());
    }

    /**
     * 是否有管理员角色
     *
     * @return 结果
     */
    public boolean hasAdminRoleKey(Set<String> roles) {
        if (StringUtil.isNotNull(roles)) {
            return roles.contains(adminProperties.getKey());
        }
        return false;
    }
}
